package cn.yinzhiping.rabbbitmqlearn.consumer;

import cn.yinzhiping.rabbbitmqlearn.config.JvmCacheLinkedBlockingQueue;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * 消费者点对点模式
 * @author yinzhiping
 */
@Component               //默认的队列是持久化  非独占  不是自动删除的队列
@RabbitListener(queuesToDeclare = @Queue("hello"))
//@RabbitListener(queuesToDeclare = @Queue(value="hello",durable="true",autoDelete="true"))  设置更改参数
public class HelloWordConsumer {

    @Resource
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private JvmCacheLinkedBlockingQueue cacheLinkedBlockingQueue;


    @RabbitHandler
    public void recieveHello(String message){
        cacheLinkedBlockingQueue.putQueue(message);
        taskExecutor.execute(()->{
            try {
                System.out.println(message);
                System.out.println(Thread.currentThread().getName());
                cacheLinkedBlockingQueue.execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //System.out.println(cacheLinkedBlockingQueue);
        });
    }
}
